module unpack();

initial begin
    // Unpacking an int into byte variables
    begin
        int val = 32'h12345678;
        byte b0, b1, b2, b3;
        {>>{b3, b2, b1, b0}} = val; // Unpacking
        $display("Unpacked bytes: b0=%h, b1=%h, b2=%h, b3=%h", b0, b1, b2, b3);
    end

    // Unpacking a packed array into an unpacked array
    begin
        bit [3:0] pk = 4'b1101;
        bit       upk[4];
        {>>{upk}} = pk; // Unpacking
        $display("Unpacked array: upk[0]=%b, upk[1]=%b, upk[2]=%b, upk[3]=%b", upk[0], upk[1], upk[2], upk[3]);
    end

    // Array of bytes into byte variables
    begin
        byte arr[4] = {8'h01, 8'h23, 8'h45, 8'h67};
        byte b0, b1, b2, b3;
        {>>{b3, b2, b1, b0}} = arr; // Unpacking
        $display("Unpacked bytes from array: b0=%h, b1=%h, b2=%h, b3=%h", b0, b1, b2, b3);
    end

    // Array of bytes into queue of bytes
    begin
        byte arr[4] = {8'h01, 8'h23, 8'h45, 8'h67};
        byte q[$];
        {>>{q}} = arr; // Unpacking
        $display("Unpacked bytes into queue: ");
        foreach (q[i]) begin
            $display("q[%0d]=%h", i, q[i]);
        end
    end

    // Reversing the elements of a byte array
    begin
        byte arr[4] = {8'h01, 8'h23, 8'h45, 8'h67};
        byte rarr[4];
        {<<byte{rarr}} = arr; // Unpacking
        $display("Original array: arr[0]=%h, arr[1]=%h, arr[2]=%h, arr[3]=%h", arr[0], arr[1], arr[2], arr[3]);
        $display("Reversed array: rarr[0]=%h, rarr[1]=%h, rarr[2]=%h, rarr[3]=%h", rarr[0], rarr[1], rarr[2], rarr[3]);
    end

    // Unpack an array into the fields of a structure
    begin
        typedef struct {
            bit [3:0] addr;
            bit [3:0] data;
        } packet_t;

        packet_t packet;
        bit [1:0] arr [4] = {2'b00, 2'b01, 2'b10, 2'b11};
        {>>{packet.addr, packet.data}} = arr; // Unpacking
        $display("Unpacked struct: addr=%b, data=%b", packet.addr, packet.data);
    end
end

endmodule
